﻿using Microsoft.AspNetCore.Mvc;
using NETCore.Encrypt;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using FluentValidation;

namespace Helpers
{
    public class EncryptInput
    {
        public string Data { get; set; }
    }

    /// <summary>
    /// 默认使用
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class EncryptInput<T> : EncryptInput where T : class
    {
        /// <summary>
        /// 获取Data解密后的对象
        /// </summary>
        /// <returns></returns>
        public T DecryptData()
        {
            var obj = ActionResultHelper.Decrypt<T>(Data);
            return obj;
        }

        /// <summary>
        /// 获取Data解密后的对象,并验证数据
        /// </summary>
        /// <typeparam name="T2">AbstractValidator</typeparam>
        /// <returns></returns>
        public T DecryptAndValidData<T2>() where T2 : AbstractValidator<T>, new()
        {
            var obj = ActionResultHelper.Decrypt<T>(Data);
            var validator = Activator.CreateInstance<T2>();
            validator.ValidateAndThrow(obj);
            return obj;
        }
    }
}
